﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager : MonoSingleton<UIManager>
{
    private Canvas fixCanvas;
    private Canvas uiCanvas;
    private Canvas popupCanvas;
    private Camera uiCamera;

    public Canvas FixCanvas
    {
        get
        {
            return fixCanvas;
        }
    }

    public Canvas UICanvas
    {
        get
        {
            return uiCanvas;
        }
    }

    public Canvas PopupCanvas
    {
        get
        {
            return popupCanvas;
        }
    }

    public Camera UICamera
    {
        get
        {
            return uiCamera;
        }
    }
   

    private Stack<GameObject> windowsStack = new Stack<GameObject>();

    private void Awake()
    {
        uiCanvas = GameObject.Find("UICanvas").GetComponent<Canvas>();
        popupCanvas = GameObject.Find("PopupCanvas").GetComponent<Canvas>();
        fixCanvas = GameObject.Find("FixCanvas").GetComponent<Canvas>();
        uiCamera = GameObject.Find("UICamera").GetComponent<Camera>();
    }

    public string GetUIFullName(string name)
    {
        return "Prefabs/UI/" + name;
    }

    public XLua.LuaTable OpenFixUI(string name)
    {
        GameObject ui = ResourceManager.Instance.LoadPrefabs(GetUIFullName(name)) as GameObject;
        if (ui == null)
        {
            Debug.LogError("asset not found!");
            return null;
        }
        var newUI = Instantiate(ui, fixCanvas.transform);
        var luaBehaviour = newUI.GetComponent<LuaBehaviour>();
        return luaBehaviour.GetScriptEnv();
    }

    public string GetFullWindowName(string winName)
    {
        return "Prefabs/View/" + winName;
    }
    public void PopWindow()
    {
        if(windowsStack.Count<=0)
        {
            return;
        }
        var oldWindows = windowsStack.Pop();
        if(oldWindows)
        {
            Destroy(oldWindows);
        }
    }

    public XLua.LuaTable PushWindow(string winName)
    {
        GameObject windows = ResourceManager.Instance.LoadPrefabs(GetFullWindowName(winName)) as GameObject;
        if (windows == null)
        {
            Debug.LogError("asset not found!");
            return null;
        }
        var newWindows = Instantiate(windows, uiCanvas.transform);
        windowsStack.Push(newWindows);
        var luaBehaviour = newWindows.GetComponent<LuaBehaviour>();
        return luaBehaviour.GetScriptEnv();
    }

    public XLua.LuaTable OpenWindows(string winName)
    {
        return PushWindow(winName);
    }

    public string GetFullPopupName(string winName)
    {
        return "Prefabs/Popup/" + winName;
    }

    public XLua.LuaTable OpenPopup(string name)
    {
        var popup = ResourceManager.Instance.LoadPrefabs(GetFullPopupName(name));
        if(popup==null)
        {
            Debug.LogError("asset not found!");
            return null;
        }
        var newpopup = Instantiate(popup as GameObject, popupCanvas.transform);
        var luaBehaviour = newpopup.GetComponent<LuaBehaviour>();
        return luaBehaviour.GetScriptEnv();
    }

    public Vector2 ScreenPointToLocalPointInRectangle(RectTransform rect,Vector2 screenPoint,Camera cam)
    {
        Vector2 outPos= new Vector2(0, 0);
        RectTransformUtility.ScreenPointToLocalPointInRectangle(rect, screenPoint, cam,out outPos);
        return outPos;
    }
}
